<?php

namespace app\modules\Order\models;

use Yii;

/**
 * This is the model class for table "order_price".
 *
 * @property integer $id
 * @property string $desc
 * @property string $price
 * @property integer $num
 * @property string $total_price
 * @property integer $status
 * @property integer $user_id
 * @property string $created_at
 * @property string $updated_at
 */
class OrderPrice extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_price';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['price', 'total_price'], 'number'],
            [['num', 'status', 'user_id', 'order_id', 'type'], 'integer'],
            [['created_at', 'updated_at', 'customers'], 'safe'],
            [['desc'], 'string', 'max' => 255],
            //[[''], 'string', 'max' => 500],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'desc' => Yii::t('app', 'Desc'),
            'price' => Yii::t('app', 'Price'),
            'num' => Yii::t('app', 'Num'),
            'total_price' => Yii::t('app', 'Total Price'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'order_id' => Yii::t('app', 'Order ID'),
            'type' => Yii::t('app', 'Type'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }

	public function getTypeObj(){//{{{
		
       	return $this->hasOne(OrderPriceType::className(), ['id' => 'type']);
	}//}}}

	public function getOrder(){
		return $this->hasOne(Order::className(), ['id' => 'order_id']);
	}

	// 保存前执行
    public function beforeSave($insert) {//{{{
        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;

            $this->status = 1;
        }
		if(!empty($this->customers)){
			$this->num = count($this->customers);		
		}else{
			//$this->num = 0;		
			$this->addError('num', '请选择要添加的客户');
			return false;
		}
		
		if($this->type){
			$priceType = OrderPriceType::findOne($this->type);
			$this->price = $priceType->price;
		}else{
			// 必须选择附加费的类型				
			$this->addError('type', '类型必须选择');
			return false;
		}

		$this->customers = json_encode($this->customers);


		$this->total_price = ($this->price * $this->num);
		//print_r($this->getAttributes()); exit;
        return parent::beforeSave($insert);
    }//}}}

	// 保存后执行
	public function afterSave($insert, $changedAttributes){//{{{
		parent::afterSave($insert, $changedAttributes);

		$order = Order::findOne($this->order_id);
		if($insert) {
			$order->price = $order->price + $this->total_price;
			$order->save();
			//这里是新增数据
		} else {
			//这里是更新数据
			if(isset($changedAttributes['total_price'])){ // 说明总价格有变动
				$price = $changedAttributes['total_price'] - $this->total_price;
				$order->price = $order->price - $price;
				$order->save();
			}
		}
		// 操作日志
		if($insert) {
			$desc = sprintf("添加附加费  %s" , $this->typeObj->name);
			Yii::$app->logs->info($this->order_id, 6, $desc);
		}
	}//}}}

	// 删除后执行
	public function afterDelete(){//{{{
		parent::afterDelete();

		//  修改对应订单的价格
		$order = Order::findOne($this->order_id);
		$order->price = $order->price - $this->total_price;
		$order->save();
	}//}}}


}
